package com.lmb.todo.controller;

import com.lmb.todo.pojo.Todo;
import com.lmb.todo.pojo.resp.Code;
import com.lmb.todo.pojo.resp.CommonResp;
import com.lmb.todo.service.GroupService;
import com.lmb.todo.service.TodoService;
import com.lmb.todo.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author lmb
 */
@RestController
@RequestMapping("/api/todo")
public class TodoController {

    @Autowired
    private TodoService todoService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private AuthUtil authUtil;

    /**
     * 根据 groupId 获取 todo
     * @param groupId Integer
     */
    @GetMapping("/{groupId}")
    public CommonResp getByGroupId(@PathVariable Integer groupId) {
        List<Todo> todoList = todoService.getByGroupId(groupId);
        return new CommonResp(
            todoList.size() > 0 ? todoList : new ArrayList<>(),
            Code.HTTP_SUCCESS,
            "ok!"
        );
    }

    @PutMapping("")
    public CommonResp update(@RequestBody Todo todo) {
        CommonResp resp = new CommonResp();
        Integer count = todoService.update(todo);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("ok!");
            return resp;
        }

        resp.setCode(Code.HTTP_FAILED);
        resp.setMsg("update todo failed!");
        return resp;
    }

    @PostMapping("/done")
    public CommonResp updateDone(@RequestBody List<Integer> data) {
        CommonResp resp = new CommonResp();
        Integer todoId = data.get(0);
        Integer status = data.get(1);

        if ((status != 1 && status != 0) || todoId < 0) {
            resp.setMsg("Parameter error!");
            resp.setCode(Code.C_UPDATE_ERROR);
            return resp;
        }

        Integer count = todoService.updateDone(todoId, status);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("ok!");
            return resp;
        }

        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("done failed!");
        return resp;
    }

    @PostMapping("/star")
    public CommonResp updateStar(@RequestBody List<Integer> data) {
        CommonResp resp = new CommonResp();
        Integer todoId = data.get(0);
        Integer status = data.get(1);

        if ((status != 1 && status != 0) || todoId < 0) {
            resp.setMsg("Parameter error!");
            resp.setCode(Code.C_UPDATE_ERROR);
            return resp;
        }

        Integer count = todoService.updateStar(todoId, status);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("ok!");
            return resp;
        }

        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("star failed!");
        return resp;
    }

    @PostMapping("/move")
    public CommonResp updateGroup(@RequestBody List<Integer> data) {
        CommonResp resp = new CommonResp();
        Integer todoId = data.get(0);
        Integer groupId = data.get(1);
        Integer count = todoService.updateGroupId(todoId, groupId);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("ok!");
            return resp;
        }

        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("move to group failed!");
        return resp;
    }

    @DeleteMapping("/{id}")
    public CommonResp deleteTodo(@PathVariable Integer id) {
        CommonResp resp = new CommonResp();
        Integer count = todoService.deleteTodo(id);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("ok!");
            return resp;
        }

        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("delete todo failed!");
        return resp;
    }

    @PostMapping("")
    public CommonResp add(@RequestBody Todo todo){
        CommonResp resp = new CommonResp();
        Integer count = todoService.add(todo, true);

        if (count > 0) {
            resp.setCode(Code.HTTP_SUCCESS);
            resp.setMsg("ok!");
            return resp;
        }
        resp.setCode(Code.S_UNKNOWN_ERROR);
        resp.setMsg("add failing.");
        return resp;
    }

    @GetMapping("/search/{content}")
    public CommonResp search(@PathVariable String content){
        CommonResp resp = new CommonResp();
        HashMap userInfo = authUtil.getAuthUserInfo();
        Integer userId = (Integer) userInfo.get("userId");

        content = content.trim();
        if ("".equals(content)) {
            resp.setMsg("Search string error!");
            resp.setCode(Code.C_SELECT_ERROR);
            return resp;
        }

        List<Integer> groupIdList = groupService.getGroupIdsByUserId(userId);
        List<Todo> todoList = todoService.getTodoByContent(content, groupIdList);

        resp.setCode(Code.HTTP_SUCCESS);
        resp.setData(todoList);
        resp.setMsg("ok!");
        return resp;
    }
}
